package com.example.piece.service

import android.accessibilityservice.AccessibilityService
import android.os.Build
import android.view.accessibility.AccessibilityEvent
import androidx.annotation.RequiresApi
import com.example.base.broadcast.Event
import com.example.base.broadcast.EventBusUtil
import com.example.base.tools.TimerUtil
import com.example.base.ui.view.broccoli.util.LogUtil
import com.example.piece.constant.Key
import com.example.piece.tools.ClickUtils
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MyAccessibilityService : AccessibilityService() {
    override fun onAccessibilityEvent(accessibilityEvent: AccessibilityEvent?) {

        LogUtil.logE(accessibilityEvent.toString())
    }

    var timer: TimerUtil.Timer? = null
    override fun onInterrupt() {
    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun onServiceConnected() {
        super.onServiceConnected()

        EventBusUtil.register(this);

        LogUtil.logE("dddddddd")
        timer = TimerUtil.Timer()

        timer?.timer({}, {
            LogUtil.logE("gggg")

            LogUtil.logE("fffff")

        }, 3, 60 * 1000)

    }

    override fun onDestroy() {
        super.onDestroy()
        EventBusUtil.unregister(this)

        timer?.cancel()
    }

    @RequiresApi(Build.VERSION_CODES.N)
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventBusCome(event: Event<Any>?) {
        if (event != null) {
            if (event.code == Key.EventKey.EVENT_DOUYIN_MSG) {
                val s = event.data as String
                when (s) {
                    "暂停播放" -> {
                        ClickUtils.click(this, 300f, 300f, 2000f, 2000f, 0, 100)

                    }
                    "下个视频" -> {
                        ClickUtils.click(this, 300f, 310f, 2000f, 200f)

                    }
                    "返回上个" -> {
                        ClickUtils.click(this, 300f, 310f, 500f, 2000f)

                    }
                }
            }
        }
    }
}